const devConfig = require('../config')
const proxy = devConfig.devComm.proxy
const STATUS_SUCCESS = 0
const STATUS_FAIL = 1

function buildProxy(){
    const cases = {}
    require('fs').readdirSync(require('path').join(`${__dirname}/apis`))
    .forEach(file=>{
        try{
            Object.assign(cases,require(`./apis/${file}`))
        }catch(e){
            console.log(e)
        }
    })
    const proxyObj = {}
    Object.keys(cases).forEach(key=>{
        proxyObj[key] = (req,res)=>{
            const succ = (data={})=>{
                res.json({status:STATUS_SUCCESS,success:true,payload:data})
            }
            const fail = (data={})=>{
                res.json({status:STATUS_FAIL,success:false,payload:data})

            }
            cases[key]({req,res,succ,fail,body:req.body,params:req.params,query:req.query})
        }
    })
    return proxyObj
}

module.exports = (proxy?{}:buildProxy())